/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Cliente.java
 *
 * Created on 22/04/2011, 01:17:44
 */

package tp1.views;

import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import tp1.controllers.Controller;
import tp1.dao.ChamadaDao;

/**
 *
 * @author tulio
 */
public class ChamadaPanel extends javax.swing.JPanel {

    /** Creates new form Cliente */
    public ChamadaPanel() {
        initComponents();
        initGrid();
        initToolbar();
    }

    private void initToolbar(){
        Controller controllerChamada = Controller.factory(Controller.CONTROLLER_CHAMADA, this);

        controllerChamada.addEvent(buttonSalvar, Controller.EVENTO_SALVAR_CHAMADA);
        controllerChamada.addEvent(buttonNovo, Controller.EVENTO_NOVO_CHAMADA);
        controllerChamada.addEvent(buttonEditar, Controller.EVENTO_EDITAR_CHAMADA);
        controllerChamada.addEvent(buttonApagar, Controller.EVENTO_APAGAR_CHAMADA);
        controllerChamada.addEvent(buttonBuscar, Controller.EVENTO_BUSCAR_CHAMADA);
       
        
        buttonSalvar.addActionListener(controllerChamada);
        buttonNovo.addActionListener(controllerChamada);
        buttonApagar.addActionListener(controllerChamada);
        buttonEditar.addActionListener(controllerChamada);
        buttonBuscar.addActionListener(controllerChamada);
       
    }

    public void updateGridData() throws SQLException, Exception{
        Object[][] data = ChamadaDao.listAsVector();
        this.setGridData(data);
    }

    public void setGridData(Object[][] data) {
        DefaultTableModel model = (DefaultTableModel) gridChamada.getModel();
        model.setDataVector(data, new String [] {
            "ID","Número de Origem", "Número de Destino", "Inicio","Fim"
        });
    }

    private void initGrid(){
        try {
            /**
             * Define os dados do grid
             */
            updateGridData();

            /**
             * Listener para a seleção de linhas no grid - captura valores e define-os no formulário
             */
            gridChamada.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    if(gridChamada.getModel().getRowCount() > gridChamada.getSelectedRow() && gridChamada.getSelectedRow() != -1){
                        Object id = gridChamada.getModel().getValueAt(gridChamada.getSelectedRow(), 0);
                        Object origem = gridChamada.getModel().getValueAt(gridChamada.getSelectedRow(), 1);
                        Object destino = gridChamada.getModel().getValueAt(gridChamada.getSelectedRow(), 2);
                        Object inicio = gridChamada.getModel().getValueAt(gridChamada.getSelectedRow(), 3);
                        Object fim = gridChamada.getModel().getValueAt(gridChamada.getSelectedRow(), 4);

                        fieldOrigem.setEditable(false);
                        fieldOrigem.setText(origem.toString());
                        fieldDestino.setEditable(false);
                        fieldDestino.setText(destino.toString());
                        fieldInicio.setText(inicio.toString());
                        fieldFim.setText(fim.toString());
                        fieldID.setEditable(false);
                        fieldID.setText(id.toString());


                    }
                }
            });
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }








    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabs = new javax.swing.JTabbedPane();
        tabGrid = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        gridChamada = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        buttonNovo = new javax.swing.JButton();
        buttonEditar = new javax.swing.JButton();
        buttonApagar = new javax.swing.JButton();
        buttonBuscar = new javax.swing.JButton();
        tabForm = new javax.swing.JPanel();
        labelInicio = new javax.swing.JLabel();
        labelFim = new javax.swing.JLabel();
        buttonSalvar = new javax.swing.JButton();
        fieldOrigem = new javax.swing.JTextField();
        fieldDestino = new javax.swing.JTextField();
        fieldID = new javax.swing.JTextField();
        labelID = new javax.swing.JLabel();
        labelDestino = new javax.swing.JLabel();
        labelOrigem = new javax.swing.JLabel();
        fieldInicio = new javax.swing.JFormattedTextField();
        fieldFim = new javax.swing.JFormattedTextField();

        setPreferredSize(new java.awt.Dimension(971, 447));

        gridChamada.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Origem", "Destino", "Inicio", "Fim"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(gridChamada);

        jToolBar1.setRollover(true);

        buttonNovo.setText("Novo");
        buttonNovo.setFocusable(false);
        buttonNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonNovo);

        buttonEditar.setText("Editar");
        buttonEditar.setFocusable(false);
        buttonEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonEditar);

        buttonApagar.setText("Apagar");
        buttonApagar.setFocusable(false);
        buttonApagar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonApagar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonApagar);

        buttonBuscar.setText("Buscar");
        buttonBuscar.setFocusable(false);
        buttonBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonBuscar);

        javax.swing.GroupLayout tabGridLayout = new javax.swing.GroupLayout(tabGrid);
        tabGrid.setLayout(tabGridLayout);
        tabGridLayout.setHorizontalGroup(
            tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabGridLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE))
                .addContainerGap())
        );
        tabGridLayout.setVerticalGroup(
            tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabGridLayout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabs.addTab("Dados", tabGrid);

        labelInicio.setText("Inicio");

        labelFim.setText("Fim");

        buttonSalvar.setText("Salvar");

        fieldID.setEditable(false);

        labelID.setText("ID");

        labelDestino.setText("Destino");

        labelOrigem.setText("Origem");

        try {
            fieldInicio.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-##-## ##:##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            fieldFim.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-##-## ##:##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout tabFormLayout = new javax.swing.GroupLayout(tabForm);
        tabForm.setLayout(tabFormLayout);
        tabFormLayout.setHorizontalGroup(
            tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabFormLayout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelInicio)
                    .addComponent(labelOrigem)
                    .addComponent(labelFim)
                    .addComponent(labelDestino)
                    .addComponent(labelID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonSalvar)
                    .addComponent(fieldOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fieldDestino, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(fieldInicio, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(fieldID, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                        .addComponent(fieldFim)))
                .addContainerGap(380, Short.MAX_VALUE))
        );
        tabFormLayout.setVerticalGroup(
            tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabFormLayout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelInicio)
                    .addComponent(fieldInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelFim)
                    .addComponent(fieldFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelOrigem))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldDestino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelDestino))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonSalvar)
                .addGap(50, 50, 50))
        );

        tabs.addTab("Formulário", tabForm);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(tabs, javax.swing.GroupLayout.PREFERRED_SIZE, 694, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(241, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(tabs, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(47, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonApagar;
    private javax.swing.JButton buttonBuscar;
    private javax.swing.JButton buttonEditar;
    private javax.swing.JButton buttonNovo;
    private javax.swing.JButton buttonSalvar;
    private javax.swing.JTextField fieldDestino;
    private javax.swing.JFormattedTextField fieldFim;
    private javax.swing.JTextField fieldID;
    private javax.swing.JFormattedTextField fieldInicio;
    private javax.swing.JTextField fieldOrigem;
    private javax.swing.JTable gridChamada;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel labelDestino;
    private javax.swing.JLabel labelFim;
    private javax.swing.JLabel labelID;
    private javax.swing.JLabel labelInicio;
    private javax.swing.JLabel labelOrigem;
    private javax.swing.JPanel tabForm;
    private javax.swing.JPanel tabGrid;
    private javax.swing.JTabbedPane tabs;
    // End of variables declaration//GEN-END:variables

    public JButton getButtonApagar() {
        return buttonApagar;
    }

    public void setButtonApagar(JButton buttonApagar) {
        this.buttonApagar = buttonApagar;
    }

    public JButton getButtonBuscar() {
        return buttonBuscar;
    }

    public void setButtonBuscar(JButton buttonBuscar) {
        this.buttonBuscar = buttonBuscar;
    }

    public JButton getButtonEditar() {
        return buttonEditar;
    }

    public void setButtonEditar(JButton buttonEditar) {
        this.buttonEditar = buttonEditar;
    }

    public JButton getButtonNovo() {
        return buttonNovo;
    }

    public void setButtonNovo(JButton buttonNovo) {
        this.buttonNovo = buttonNovo;
    }

    public JButton getButtonSalvar() {
        return buttonSalvar;
    }

    public void setButtonSalvar(JButton buttonSalvar) {
        this.buttonSalvar = buttonSalvar;
    }

    public JTextField getFieldDestino() {
        return fieldDestino;
    }

    public void setFieldDestino(JTextField fieldDestino) {
        this.fieldDestino = fieldDestino;
    }

    public JFormattedTextField getFieldFim() {
        return fieldFim;
    }

    public void setFieldFim(JFormattedTextField fieldFim) {
        this.fieldFim = fieldFim;
    }

    public JTextField getFieldID() {
        return fieldID;
    }

    public void setFieldID(JTextField fieldID) {
        this.fieldID = fieldID;
    }

    public JFormattedTextField getFieldInicio() {
        return fieldInicio;
    }

    public void setFieldInicio(JFormattedTextField fieldInicio) {
        this.fieldInicio = fieldInicio;
    }

    public JTextField getFieldOrigem() {
        return fieldOrigem;
    }

    public void setFieldOrigem(JTextField fieldOrigem) {
        this.fieldOrigem = fieldOrigem;
    }

    public JTable getGridChamada() {
        return gridChamada;
    }

    public void setGridChamada(JTable gridChamada) {
        this.gridChamada = gridChamada;
    }

    public JLabel getLabelDestino() {
        return labelDestino;
    }

    public void setLabelDestino(JLabel labelDestino) {
        this.labelDestino = labelDestino;
    }

    public JLabel getLabelFim() {
        return labelFim;
    }

    public void setLabelFim(JLabel labelFim) {
        this.labelFim = labelFim;
    }

    public JLabel getLabelID() {
        return labelID;
    }

    public void setLabelID(JLabel labelID) {
        this.labelID = labelID;
    }

    public JLabel getLabelInicio() {
        return labelInicio;
    }

    public void setLabelInicio(JLabel labelInicio) {
        this.labelInicio = labelInicio;
    }

    public JLabel getLabelOrigem() {
        return labelOrigem;
    }

    public void setLabelOrigem(JLabel labelOrigem) {
        this.labelOrigem = labelOrigem;
    }

    public JPanel getTabForm() {
        return tabForm;
    }

    public void setTabForm(JPanel tabForm) {
        this.tabForm = tabForm;
    }

    public JPanel getTabGrid() {
        return tabGrid;
    }

    public void setTabGrid(JPanel tabGrid) {
        this.tabGrid = tabGrid;
    }

    public JTabbedPane getTabs() {
        return tabs;
    }

    public void setTabs(JTabbedPane tabs) {
        this.tabs = tabs;
    }


}

